package com.wx.client;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import com.wx.exception.BusinessException;
import com.wx.properties.OssProperties;
import com.wx.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * <p>Title:	  weixin-go-with-you <br/> </p>
 * <p>Description TODO <br/> </p>
 * <p>Company:    医百科技有限公司  <br/> </p>
 *
 * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>  <br/>
 * @Date 2021/3/31 10:19
 */
@Component
@Slf4j
public class SysFileClient {

    @Resource
    private OssProperties ossProperties;

    public String uploadToTencent(MultipartFile multipartFile) {
        // 1 初始化用户身份信息
        COSCredentials cred = new BasicCOSCredentials(ossProperties.getSecretId(), ossProperties.getSecretKey());
        // 2 设置 bucket 的区域
        Region region = new Region(ossProperties.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 设置存储对象名称
        String suffix = Objects.requireNonNull(multipartFile.getOriginalFilename()).substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        String newFileName = sdf.format(new Date()) + "/" + SnowFlakeUtil.getStringId() + suffix;
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), newFileName, transferToFile(multipartFile));
        cosClient.putObject(putObjectRequest);
        String url = "/" + newFileName;
        return url;
    }


    public String uploadToTencent(File file, String fileName) {
        // 1 初始化用户身份信息
        COSCredentials cred = new BasicCOSCredentials(ossProperties.getSecretId(), ossProperties.getSecretKey());
        // 2 设置 bucket 的区域
        Region region = new Region(ossProperties.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 设置存储对象名称
        String suffix = Objects.requireNonNull(fileName).substring(fileName.lastIndexOf("."));
        String newFileName = sdf.format(new Date()) + "/" + SnowFlakeUtil.getStringId() + suffix;
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), newFileName, file);
        cosClient.putObject(putObjectRequest);
        String url = "/" + newFileName;
        return url;
    }

    private File transferToFile(MultipartFile multipartFile) {
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            assert originalFilename != null;
            String[] filename = originalFilename.split("\\.");
            file=File.createTempFile(filename[0], filename[1]);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IllegalArgumentException e) {
            throw new BusinessException("照片名字太短");
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
        return file;
    }
}
